// pages/self/myAddress/myAddress.js;
const httpNet = require("../../../utils/httpUtil.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    handbottom: '1',
    isBack:''
  },
  /**
   * 订单页面过来选择地址 点击后的事件
   */
	chooseThisAddr:function(e){
		var ch = this.data.isOrdCho;
		if (ch == 1) {
			const addr = e.currentTarget.dataset.addr;
			console.log(addr);
			var that = this;
			var pages = getCurrentPages(); // 获取页面栈
			var currPage = pages[pages.length - 1]; // 当前页面
			var prevPage = pages[pages.length - 2]; // 上一个页面

			console.log(addr.province + addr.city + addr.area + addr.addressInfo);
			prevPage.setData({
				obj: {
					province: '',
					city: '',
					area: '',
					addressInfo: addr.province + addr.city + addr.area + addr.addressInfo,
					userName: addr.userName,
					mobilePhone: addr.mobilePhone,
					addrId: addr.id,
					lat: addr.latitude,
					lng: addr.longitude,
				},
				noAddress:false,
			});
			prevPage.showOrderInfo();
			wx.setStorageSync('orderAddress-' + wx.getStorageSync('orderShopId'), prevPage.data.obj);
			wx.navigateBack({
				delta: 1
			})
		}
		
	},
  editAddress:function(e){
    // 获取当前地址id
    const id = e.currentTarget.dataset.id;
    // 跳转至新增页面
    console.log(this.data.handbottom);
    console.log(e.currentTarget.dataset.item);
    const ressparam = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../../self/addRess/addRess?id=' + ressparam.id + '&userName=' + ressparam.userName + '&mobilePhone=' + ressparam.mobilePhone + '&province=' + ressparam.province + '&city=' + ressparam.city + '&area=' + ressparam.area + '&addressInfo=' + ressparam.addressInfo + '&longitude=' + ressparam.longitude + '&latitude=' + ressparam.latitude + '&gender=' + ressparam.gender
    })
  },
  // 设置为默认地址
  setDefault:function(e){
    var that = this;
    const uid = this.data.uid;
    const id = e.currentTarget.dataset.id;
    httpNet.reqPost('xwx/updateDefaultAddress', { userId: uid, id: id},       
      function(res){
      console.log(res)
      if (res.resultCode === 1000){
        upData(1, 10, uid, that);
      }else{
        wx.showToast({
          title: '设置失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  // 删除地址
  delectRess:function(e){
    var that = this;
    const uid = this.data.uid;
    const id = e.currentTarget.dataset.id;
    that.setData({
      iddelect:1
    });
    httpNet.reqPost('xwx/updateUserAddres', { userId: uid, id: id, status:0 },
      function (res) {
        console.log(res)
        if (res.resultCode === 1000) {
          wx.showToast({
            title: '删除成功',
            duration: 1000
          })
          upData(1, 10, uid, that);
        } else {
          wx.showToast({
            title: '删除失败',
            icon: 'none',
            duration: 2000
          });
        }
      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
///判断是否是从订单过来选择的
	var isOrd =   options.isOrdCho;
	if(isOrd == 1){
		this.setData({
			isOrdCho:1
		})
	}
///
 const that = this;
    try{
      var value = wx.getStorageSync('uid')
      if (value) {
        // Do something with return value
        console.log(value)
        that.setData({
          uid: value
        })
      }
    } catch (e){
      wx.showToast({
        title: '请前往「我的」授权登陆',
        icon: 'none',
        duration: 2000
      });
    };
    const id = this.data.uid;
    console.log(id)
    upData(1, 10, id, that);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    // 监听返回
    console.log(that.data)
    if(that.data.isBack.back == 1){
      const id = this.data.uid;
      console.log(id)
      upData(1, 10, id, that);
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.handbottom++;
    this.setData({
      iddelect: 0
    })
    upData(this.data.handbottom, 10, this.data.uid, this)
    console.log(this.data.handbottom)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
});
function upData(page,pageSize,id,that) {
  wx.showLoading({
    title: '加载中',
  });
  page?page:1;
  pageSize?pageSize:10;
  console.log(id)
  httpNet.reqPost('xwx/getUserAddressList', { userId: id, pageNum: page, pageSize: pageSize},function(res){
      wx.hideLoading();
      console.log(res)
    if (res.resultCode === 1000){
      if (res.returnObject != null){
        if (res.returnObject.list.length != 0){
          that.setData({
            pageList: res.returnObject.list
          })
        }
      }else{
        if (that.data.iddelect == 1){
          that.setData({
            pageList: ""
          });
        }
        // wx.showToast({
        //   title: '暂无数据展示',
        //   icon: 'none',
        //   duration: 2000
        // })
      }
    }else{
      wx.showToast({
        title: '加载失败',
        icon: 'none',
        duration: 2000
      })
    }
  })
}